import java.io.*;
import java.util.*;

public class Main {

    public static final String DEST_DIR = "D:\\Guoshu\\QichengResource\\images";

    public static Map<String, List<String>> fileCache = new HashMap<>();

    public static Map<String, List<FileDetail>> fileDetailCache = new HashMap<>();

    public static int imageFileCount = 0;

    public static void main(String[] args) {
        imageFileCount = 0;

        File manifestFile = new File("resoruce.manifest");
        if(manifestFile.exists()) {
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(manifestFile)));
                String line;
                while((line = reader.readLine()) != null) {
                    line = line.replace("'", "");
                    String dir = line.endsWith(",") ? line.substring(0, line.length() - 1) : line;
                    System.out.println(dir);
                    scanDirectory(dir);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void scanDirectory(String dirPath) {
        File resDir = new File(dirPath);
        if(resDir.isDirectory() && resDir.exists()) {
            File[] files = resDir.listFiles();
            for(File f : files) {
                if(f.isDirectory() && f.getName().startsWith("drawable")) {
                    scanImageResource(f);
                }
            }
        }
    }

    private static void scanImageResource(File imageResDir) {
        File[] files = imageResDir.listFiles();
        for(File f : files){
            if(f.isFile() && isImageFile(f)) {
                imageFileCount++;
                String originalFileName = f.getName();
                String absolutePath = f.getAbsolutePath();
                String finalName;
                if(fileDetailCache.containsKey(originalFileName)) {
                    List<FileDetail> fileDetails = fileDetailCache.get(originalFileName);
                    finalName = FileUtils.appendFileName(originalFileName, String.valueOf(fileDetails.size()));
                    fileDetails.add(new FileDetail(absolutePath, finalName));
                } else {
                    finalName = originalFileName;
                    List<FileDetail> newList = new ArrayList<>();
                    newList.add(new FileDetail(absolutePath, finalName));
                    fileDetailCache.put(originalFileName, newList);
                }

                System.out.println(String.format("- [%s] %s -> %s", imageFileCount, absolutePath, finalName));
                File destFile = new File(DEST_DIR,finalName);
                copyFile(absolutePath, destFile.getAbsolutePath());
            }
        }
    }

    private static boolean isImageFile(File file){
        String fileName = file.getName();
        return fileName.endsWith(".png") || fileName.endsWith(".jpg") || fileName.endsWith(".jpeg");
    }

    private static boolean copyFile(String sourcePath, String destDirPath) {
        try {
            File source = new File(sourcePath);
            File dest = new File(destDirPath);
            InputStream is = new FileInputStream(source);
            OutputStream os = new FileOutputStream(dest);
            byte[] buffer = new byte[256];
            while(is.read(buffer) != -1) {
                os.write(buffer);
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

    static class FileDetail {
        String originalFilePath;
        String modifiedFileName;

        public FileDetail(String originalFilePath, String modifiedFileName) {
            this.originalFilePath = originalFilePath;
            this.modifiedFileName = modifiedFileName;
        }
    }

}
